package org.zachary.prosocialbehaviorservice.infrastructure.repository;

import jakarta.persistence.EntityNotFoundException;
import org.springframework.stereotype.Repository;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity.ActivityItem;
import org.zachary.prosocialbehaviorservice.domain.aggregate_model.activity.ActivityToItem;
import org.zachary.prosocialbehaviorservice.domain.repository.ActivityItemRepository;

import java.util.List;

@Repository
public class JpaActivityItemRepository implements ActivityItemRepository {
    private final SpringDataActivityItemRepository springDataActivityItemRepository;

    public JpaActivityItemRepository(SpringDataActivityItemRepository springDataActivityItemRepository) {
        this.springDataActivityItemRepository = springDataActivityItemRepository;
    }

    @Override
    public List<ActivityItem> findAllById(List<ActivityToItem> relations) {
        List<Integer> itemIds = relations.stream()
                .map(ActivityToItem::getItemId)
                .toList();
        return springDataActivityItemRepository.findAllById(itemIds);
    }

    @Override
    public void save(ActivityItem item) {
        springDataActivityItemRepository.save(item);
    }

    @Override
    public void deleteById(Integer itemId) {
        springDataActivityItemRepository.deleteById(itemId);
    }

    @Override
    public ActivityItem findById(Integer itemId) {
        return springDataActivityItemRepository.findById(itemId).orElse(null);
    }

    @Override
    public List<ActivityItem> findByIds(List<Integer> ids) {
        return springDataActivityItemRepository.findByIdIn(ids);
    }

    @Override
    public List<ActivityItem> findAll() {
        return springDataActivityItemRepository.findAll();
    }

    @Override
    public void decrementByOne(Integer id) {
        springDataActivityItemRepository.decrementNumberById(id);
    }


}
